/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.webframework.common.util;

import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

/**
 * 功能描述：属性加载读取，与PropertyLoader类似，与之不同的是少了保存属性文件这个功能，动态加载属性
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>
 * Created on 2007-5-24
 */
public class PropertyReader {

	private static Log _log = LogFactory.getLog(PropertyReader.class);

	private static Hashtable<String, Properties> pptContainer = new Hashtable<String, Properties>();

	/**
	 * 方法描述:获得属性
	 * @param propertyFilePath 属性文件(包括类路径)
	 * @param key 属性键
	 * @return 属性值
	 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>
	 */
	public static String getValue(String propertyFilePath, String key) {

		Properties ppts = getProperties(propertyFilePath);
		return ppts == null ? null : ppts.getProperty(key);
	}

	/**
	 * 方法描述:获得属性文件的属性
	 * @param propertyFilePath 属性文件(包括类路径)
	 * @return 属性
	 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>
	 */
	public static Properties getProperties(String propertyFilePath) {

		Properties ppts = pptContainer.get(propertyFilePath);
		if (ppts == null) {
			ppts = loadPropertyFile(propertyFilePath);
			if (ppts != null) {
				pptContainer.put(propertyFilePath, ppts);
			}
		}
		return ppts;
	}

	/**
	 * 方法描述: 加载属性
	 * @param propertyFilePath 属性文件(包括类路径)
	 * @return 属性
	 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>
	 */
	private static Properties loadPropertyFile(String propertyFilePath) {

		try {
			java.io.InputStream is = PropertyReader.class
					.getResourceAsStream(propertyFilePath);
			Properties ppts = new Properties();
			ppts.load(is);
			return ppts;
		} catch (Exception e) {
			_log.debug("加载属性文件出错:" + propertyFilePath, e);
			return null;
		}
	}

	/**
	 * 方法描述:设置属性值
	 * @param propertyFilePath 属性文件(包括类路径)
	 * @param key 属性键
	 * @param value 属性值
	 * @return
	 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>
	 */
	public static boolean setValue(String propertyFilePath, String key,
			String value) {

		Properties ppts = getProperties(propertyFilePath);
		if (ppts == null) {
			return false;
		}
		ppts.put(key, value);
		return true;
	}

	/**
	 * 方法描述:删除属性值
	 * @param propertyFilePath 属性文件(包括类路径)
	 * @param key 属性键
	 * @return 属性值
	 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>
	 */
	public static String removeValue(String propertyFilePath, String key) {

		Properties ppts = getProperties(propertyFilePath);
		if (ppts == null) {
			return null;
		}
		return (String) ppts.remove(key);
	}

	/**
	 * 方法描述:移除加载的属性文件
	 * @param propertyFilePath 属性文件(包括类路径)
	 * @return
	 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>
	 */
	public static Properties removePropertyFile(String propertyFilePath) {

		return pptContainer.remove(propertyFilePath);
	}

	/**
	 * 方法描述:获得属性文件的路径
	 * @param pkg 包名
	 * @param propertyFileName 属性文件名
	 * @return
	 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>
	 */
	public static String getPpropertyFilePath(String pkg,
			String propertyFileName) {

		pkg = pkg == null ? "" : pkg.replaceAll("\\.", "/");
		propertyFileName = propertyFileName.endsWith(".properties")
																	? propertyFileName
																	: (propertyFileName + ".properties");
		return "/" + pkg + "/" + propertyFileName;
	}

	/**
	 * 用于读取指定路径下的所有文件
	 * @param regString
	 * @return
	 * @author kevin
	 */
	 public static String[] getFile(String regString){
	       String[] resouceArray=null;
			try {
				ResourcePatternResolver rpr=new PathMatchingResourcePatternResolver();
				Resource[] rs=rpr.getResources(regString);
				if(rs==null){
					return null;
				}
				resouceArray=new String[rs.length];
				int i=0;
				for(Resource c:rs){
					String realPath=c.getURL().getPath();
					String xdpath=realPath.substring(realPath.indexOf("classes")+8);
					resouceArray[i]=xdpath;
					i++;
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
	   return resouceArray;
 }
}